<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const random = (n) => Math.floor(Math.random() * (n + 1));

        // 可生成重复的随机验证码
        function randomCode() {
            return [0, 0, 0, 0, 0, 0].map(() => random(9));
        }

        // 不可生成重复的随机验证码
        const shuffle = (list) => list.sort((x, y) => Math.random() - 0.5);
        const randomUniqueCode = () =>
            shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).slice(0, 6);

        // 一种比较低效的解法
        function randomUniqueCode2() {
            let i = 0;
            let l = [];
            while (i < 6) {
                const x = random(9);
                if (!l.includes(x)) {
                    i++;
                    l.push(x);
                }
            }
            return l;
        }
    </script>
</body>

</html>